package com.bt.lms.ui.action.teaching.plan;

import com.alibaba.fastjson.JSON;
import com.bt.lms.constants.TeachingPlanBizTypeConstant;
import com.bt.lms.entity.TeachingPlanBasicInfo;
import com.bt.lms.ui.action.teaching.plan.utils.GetNet;
import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.ui.page.common.action.pageteaching.TeachingPlanLabAction;
import com.bt.lms.ui.page.teaching.plan.PageEditTeachingPlanBasicInfo;
import com.bt.lms.ui.page.teaching.plan.PageTeachingPlanBasicInfoList;
import com.bt.lms.ui.page.teaching.plan.vo.EditTeachingPlanVo;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.net.Socket;

/**
 * 教学计划
 * 编辑教学计划
 * 保存并进行下一步按钮事件
 */
public class LEditTeachingPlanSaveBtnAction implements EventHandler {

    private PageEditTeachingPlanBasicInfo edit;

    private String errors;

    public LEditTeachingPlanSaveBtnAction(PageEditTeachingPlanBasicInfo pageEditTeachingPlanBasicInfo) {
        this.edit = pageEditTeachingPlanBasicInfo;
    }

    @Override
    public void handle(Event event) {

        //获取用户输入
        EditTeachingPlanVo editTeachingPlanVo = getInParams();
        //验证用户输入
        String validateMsg = validateInParam(editTeachingPlanVo);

        //组包
       byte[] msgByte;
        if (null==validateMsg){
            //验证通过
            msgByte = handle2ReqMsgPacket(editTeachingPlanVo);
        }else {
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setContentText(errors);
            alert.show();

        }
        //组网
        Socket socket = GetNet.getNet();
        //发送消息



        //要秀的界面
        PageTeachingPlanBasicInfoList pageTeachingPlanBasicInfo = new PageTeachingPlanBasicInfoList();
        Pane pane = TeachingPlanLabAction.pageSkipInfo.getPane();
        Integer index = TeachingPlanLabAction.pageSkipInfo.getIndex();
        pane.getChildren().set(index,pageTeachingPlanBasicInfo);

    }


    /**
     * 3、组包
     * @param editTeachingPlanVo
     * @return
     */
    private byte[] handle2ReqMsgPacket(EditTeachingPlanVo editTeachingPlanVo){

        //请求消息
        ReqMsg reqMsg = new ReqMsg();
        reqMsg.setBizType(TeachingPlanBizTypeConstant.PLAN_EDIT);
        //vo转entity
        TeachingPlanBasicInfo teachingPlanBasicInfo = new TeachingPlanBasicInfo();
        String jsonStr = null;
        try {
            BeanUtils.copyProperties(teachingPlanBasicInfo,editTeachingPlanVo);
            jsonStr = JSON.toJSONString(teachingPlanBasicInfo);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        reqMsg.setData(jsonStr);
        byte[] msgByte = reqMsg.toBytes();

        return msgByte;

    }



    /**
     * 2、验证用户输入
     * @param editTeachingPlanVo 用户输入的信息
     * @return
     */
    private String validateInParam(EditTeachingPlanVo editTeachingPlanVo) {

        errors = "";

        //教学计划编号
        String code = editTeachingPlanVo.getCode();
        int codeL = code.length();
        if (code.equals("")){
            errors += "计划编号必须填写"+"\r\n";
        }else if (codeL>32){
            errors += "计划编号最多填32个字"+"\r\n";
        }

        //教学计划名称
        String name = editTeachingPlanVo.getName();
        int nameL = name.length();
        if (name.equals("")){
            errors +="教学计划名称必须填写"+"\r\n";
        } else if (nameL>32){
            errors +="教学计划名称最多填32个字"+"\r\n";
        }

        //学制
        String lengthOfSchoolingText = editTeachingPlanVo.getLengthOfSchoolingText();
        //学制下拉框
        Integer lengthOfSchooling = editTeachingPlanVo.getLengthOfSchooling();

        try {
            int parseInt = Integer.parseInt(lengthOfSchoolingText);
            if (lengthOfSchooling==1&&parseInt>4){
                errors += "学制最多四年"+"\r\n";
            }else if (lengthOfSchooling==2&&parseInt>48){
                errors += "学制最多48个月"+"\r\n";
            }else if (lengthOfSchooling==3&&parseInt>35040){
                errors += "学制最多35404学时"+"\r\n";
            }
        }catch (NumberFormatException e ){
            errors += "学制必须填写数字"+"\r\n";
        }


        //可重修次数
        String numberOfRepairableTimes = editTeachingPlanVo.getNumberOfRepairableTimes();
        try {
            int parseInt = Integer.parseInt(numberOfRepairableTimes);
            if (parseInt>3){
                errors += "可重修次数最多3次";
            }
        }catch (NumberFormatException e ){
            errors += "可重修次数必须填写数字"+"\r\n";
        }


        if (errors.equals("")){
            errors = null;
        }


        return errors;
    }

    /**
     * 1、获取用户输入
     * @return
     */
    private EditTeachingPlanVo getInParams() {

        //封装成VO对象
        EditTeachingPlanVo editTeachingPlanVo = new EditTeachingPlanVo();

        TextField codeTF = (TextField) edit.lookup("#codeTF");//计划编号
        String codeStr = codeTF.getText().trim();
        editTeachingPlanVo.setCode(codeStr);

        TextField nameTF = (TextField) edit.lookup("#nameTF");//教学计划名称
        String nameStr = nameTF.getText().trim();
        editTeachingPlanVo.setName(nameStr);

        ComboBox belToCollegeCB = (ComboBox) edit.lookup("#belToCollegeCB");//所属的学习中心
        String belToCollegeCBValue = (String) belToCollegeCB.getValue();
        int belToCollegeValue = -1;
        switch (belToCollegeCBValue){
            case "选择招生院校":
                belToCollegeValue = 1;
                break;
            case "清华大学" :
                belToCollegeValue = 2;
                break;
            case "师范大学":
                belToCollegeValue = 3;
                break;
            case "北京大学":
                belToCollegeValue = 4;
                break;
        }
        editTeachingPlanVo.setBelongToCollege(belToCollegeValue);

        ComboBox belongToMajor = (ComboBox) edit.lookup("#belongToMajor");//所属专业
        String toMajorValue = (String) belongToMajor.getValue();
        int toMajorV = -1;
        switch (toMajorValue){
            case "java":
                toMajorV = 1;
                break;
            case "c++":
                toMajorV = 2;
                break;
            case "c":
                toMajorV = 3;
                break;
        }
        editTeachingPlanVo.setBelongToMajor(toMajorV);

        ComboBox applyCB = (ComboBox) edit.lookup("#applyCB");//适合层次
        String applyCBValue = (String) applyCB.getValue();
        int applyCBV = 1;
        switch (applyCBValue){
            case "选择培养层次":
                applyCBV = 1;
                break;
            case "免费师范生":
                applyCBV = 2;
                break;
            case "免费师范生硕士":
                applyCBV = 3;
                break;
            case "其他":
                applyCBV = 4;
                break;
        }
        editTeachingPlanVo.setApplyLevel(applyCBV);

        TextField lengthOfSchoolingTF = (TextField) edit.lookup("#lengthOfSchoolingTF");//学制
        String lengthOfSchoolingText = lengthOfSchoolingTF.getText().trim();
        editTeachingPlanVo.setLengthOfSchoolingText(lengthOfSchoolingText);
        ComboBox lengthOfSchoolingCB = (ComboBox) edit.lookup("#lengthOfSchoolingCB");//学制下拉框
        String lengthOfSchoolingCBValue = (String) lengthOfSchoolingCB.getValue();
        int lengthOfSchoolingCBV = -1;
        switch (lengthOfSchoolingCBValue){
            case "年":
                lengthOfSchoolingCBV = 1;
                break;
            case "月":
                lengthOfSchoolingCBV = 2;
                break;
            case "学时":
                lengthOfSchoolingCBV = 3;
                break;
        }
        editTeachingPlanVo.setLengthOfSchooling(lengthOfSchoolingCBV);

        TextField numberOfRepairableTimesTF = (TextField) edit.lookup("#numberOfRepairableTimesTF");//可重修次数
        String numberOfRepairableTimesTFStr = numberOfRepairableTimesTF.getText().trim();
        editTeachingPlanVo.setNumberOfRepairableTimes(numberOfRepairableTimesTFStr);




        return editTeachingPlanVo;
    }
}
